package com.gang.mars.basic.classatt;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author gang.chen
 * @description
 * @Date 2021/7/20 13:22
 */
public class Orange03Test {

    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        Annotation annotation03 = getOrange03Annotation(Orange03.class);
        Method orange02Method = getOrange02Method(Orange02.class);

        String name = (String) orange02Method.invoke(annotation03);
        System.out.println(name);
    }


    /**
     * @param source 从Orange03 获取注解 Orange02
     * */
    public static Annotation getOrange03Annotation(Class<?> source){

        Annotation[] declaredAnnotations = source.getDeclaredAnnotations();
        for (Annotation declaredAnnotation : declaredAnnotations) {
            String name = declaredAnnotation.annotationType().getName();
            if (!name.startsWith("java")) {
                return declaredAnnotation;
            }
        }
        return null;
    }

    /**
     * @param source 获取注解Orange02方法
     * */
    public static Method getOrange02Method(Class<?> source){
        Method[] methods = source.getMethods();
        return methods[0];
    }
}
